package com.uzai.console.service.sop.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.enums.MerConfigKeyEnum;
import com.uzai.common.enums.MerConfigTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.dto.sop.wxqaddfriendconfig.WxqAddFriendConfigDto;
import com.uzai.console.entity.MerBaseConfig;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.sop.WxqAddFriendConfigService;
import com.uzai.console.vo.sop.wxqaddfriendconfig.WxqAddFriendConfigVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * SOP任务
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class WxqAddFriendConfigServiceImpl implements WxqAddFriendConfigService {

    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询加进群好友设置信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return WxqAddFriendConfigVo
     */
    public WxqAddFriendConfigVo findWxqAddFriendConfig(Long merId){

        WxqAddFriendConfigVo wxqAddFriendConfigVo = new WxqAddFriendConfigVo();

        //查询运营商CMS基础设置信息
        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(merId, MerConfigKeyEnum.WXQ_ADD_FRIEND_CONFIG.getValue());
        if (merBaseConfig != null) {
            wxqAddFriendConfigVo = JSONObject.parseObject(merBaseConfig.getVal(), WxqAddFriendConfigVo.class);
        }

        return wxqAddFriendConfigVo;
    }

    /**
     * 修改加进群好友设置信息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @return null
     */
    public void updateWxqAddFriendConfig(WxqAddFriendConfigDto wxqAddFriendConfigDto){

        List<String> wxidList = wxqAddFriendConfigDto.getWxidList();
        if(wxidList == null || wxidList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请输入微信id");
        }

        WxqAddFriendConfigVo wxqAddFriendConfigVo = new WxqAddFriendConfigVo();
        BeanUtils.copyProperties(wxqAddFriendConfigDto, wxqAddFriendConfigVo);

        MerBaseConfig merBaseConfig = merBaseConfigMapper.selectByMerId(wxqAddFriendConfigDto.getMerId(), MerConfigKeyEnum.WXQ_ADD_FRIEND_CONFIG.getValue());

        Integer now = DateUtil.getNowTime();
        //修改
        if (merBaseConfig != null){
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wxqAddFriendConfigVo)));
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.update(merBaseConfig);
        } else { //增加
            merBaseConfig = new MerBaseConfig();
            merBaseConfig.setId(IdWorker.getId());
            merBaseConfig.setMerId(wxqAddFriendConfigDto.getMerId());
            merBaseConfig.setType(MerConfigTypeEnum.PRODUCTS.getValue());
            merBaseConfig.setKey(MerConfigKeyEnum.WXQ_ADD_FRIEND_CONFIG.getValue());
            merBaseConfig.setVal(Tools.getStrEmpty(JSONObject.toJSONString(wxqAddFriendConfigVo)));
            merBaseConfig.setCreateTime(now);
            merBaseConfig.setUpdateTime(now);
            merBaseConfigMapper.insert(merBaseConfig);
        }

        //保存到Redis当中
        redisTemplate.opsForValue().set(String.format(RedisCacheKeyEnum.WXQ_ADD_FRIEND_CONFIG.getKey(), wxqAddFriendConfigDto.getMerId()), merBaseConfig.getVal());
    }
}
